package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.dto.SysDictionDto;
import net.zigin.nhi.could.waste.vo.SysDictionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.ParsingUtils;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.dao.BaseMapper;
import net.zigin.nhi.could.waste.dao.SysDictionMapper;
import net.zigin.nhi.could.waste.model.SysDiction;
import net.zigin.nhi.could.waste.service.SysDictionService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-20 22:06:06                                               
##################################################################*/


/**
 * SysDiction服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysDictionServiceImpl
 * @date 2021-9-20 22:06:06
 */
@Service
public class SysDictionServiceImpl extends AbstractServiceImpl<SysDiction, String> implements SysDictionService {
    @Autowired
    private SysDictionMapper sysDictionMapper;

    @Override
    public BaseMapper<SysDiction, String> getMapper() {
        return sysDictionMapper;
    }

    @Override
    public List<SysDictionVo> getListByType(String type) {
        Map<String, Object> paramMap = ParamsMapUtil.getInstance().addParam("type", type);
        List<SysDiction> sysDictions = sysDictionMapper.selectListByCondition(paramMap);
        if (sysDictions.isEmpty()) {
            return new ArrayList<>();
        }
        return ReflectUtil.copyProperties(sysDictions, SysDictionVo.class);
    }

    @Override
    public String saveOrUpdate(SysDictionDto sysDictionDto) {
        SysDiction sysDiction = ReflectUtil.copyProperties(sysDictionDto, SysDiction.class);
        if (StringUtil.isNullOrEmpty(sysDiction.getId())) {
            sysDiction.setId(StringUtil.get32UUID());
            sysDictionMapper.insert(sysDiction);
        } else {
            sysDictionMapper.updateByPrimaryKeySelective(sysDiction);
        }
        return sysDiction.getId();
    }

    @Override
    public List<SysDictionVo> getList(SysDictionDto sysDictionDto) {
        Map<String, Object> paramMap = getSysDictionParam(sysDictionDto);
        List<SysDiction> sysDictions = sysDictionMapper.selectListByPage(paramMap);
        if(sysDictions.isEmpty()){
            return new ArrayList<>();
        }
        return ReflectUtil.copyProperties(sysDictions, SysDictionVo.class);
    }

    @Override
    public Integer getCount(SysDictionDto sysDictionDto) {
        Map<String, Object> paramMap = getSysDictionParam(sysDictionDto);
        return sysDictionMapper.selectCount(paramMap);
    }

    private Map<String, Object> getSysDictionParam(SysDictionDto sysDictionDto) {
        return ParamsMapUtil.getInstance()
                .addParam("name", sysDictionDto.getName())
                .addParam("type", sysDictionDto.getType())
                .addParam("code", sysDictionDto.getCode())
                .addParam("pageIndex", sysDictionDto.getPageIndex())
                .addParam("pageSize", sysDictionDto.getPageSize());
    }
}